package com.sixteen.ordering_system.controller;


import com.sixteen.ordering_system.mapper.DishCartMapper;
import com.sixteen.ordering_system.pojo.Dish;
import com.sixteen.ordering_system.pojo.DishCart;
import com.sixteen.ordering_system.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sixteen
 * @since 2022-09-16
 */
@Controller
//@RequestMapping("/dish-cart")
public class DishCartController {
    @Autowired
    private DishCartMapper dishCartMapper;
    @RequestMapping("addshopping")
    public String shopping(Integer dishId, HttpSession session, Dish dish){
        DishCart dishCart = new DishCart();
        dish.setDishId(dishId);
        dishCart.setDish(dish);
        SysUser sysUser = (SysUser) session.getAttribute("loginUser");
        dishCart.setSysUserId(sysUser.getSysUserId());
        dishCart.setNumber(1);
        dishCart.setRemark("aaa");
        Integer integer = dishCartMapper.insertCart(dishCart);
        if (integer > 0 ) {
            return "redirect:/index";
        }
        return "redirect:/index";

    }

    @RequestMapping("deleteShop")
    public String delshop(Integer dishId,HttpSession session,Dish dish){
        DishCart dishCart = new DishCart();
        dish.setDishId(dishId);
        dishCart.setDish(dish);
        SysUser sysUser = (SysUser) session.getAttribute("loginUser");
        dishCart.setSysUserId(sysUser.getSysUserId());
        Integer integer = dishCartMapper.delNum(dishCart);
        if (integer > 0) {
            return "redirect:/shopping";
        }
        return "redirect:/shopping";
    }

    @RequestMapping("addNumber")
    public String addnum(Integer number,Integer dishCartId,DishCart dishCart){
        dishCart.setNumber(number);
        dishCart.setDishCartId(dishCartId);
//        number++;
        Integer integer = dishCartMapper.updateNum(number,dishCartId);
        return "redirect:/shopping";
    }

    @RequestMapping("reduceNumber")
    public String reduceNum(Integer number,Integer dishCartId,DishCart dishCart){
        dishCart.setNumber(number);
        dishCart.setDishCartId(dishCartId);
//        number++;
        Integer integer = dishCartMapper.updateNum(number,dishCartId);
        return "redirect:/shopping";
    }



}
